<template>
  <div>
    <el-dialog title="添加项目类型" :visible="show" :modal-append-to-body="false" :before-close="cmdClose" center @open="cmdInit" width="500px" top="10vh">
      <el-form ref="form" label-width="90px" :model="formData" :rules="ruleData">
        <el-form-item label="项目类型" prop="type">
          <el-input placeholder="请输入项目类型" v-model="formData.type"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer footBtn">
        <el-button type="primary" @click="cmdSubmit">确定</el-button>
        <el-button type="primary" @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { modifyProjectType } from '@/api/api'
import swal from 'sweetalert'
import _ from 'underscore'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formData: {
        type: ''
      },
      ruleData: {
        type: [{ required: true, message: '请输入项目类型', trigger: 'blur' }],
      }
    }
  },
  methods: {
    cmdInit () { },
    cmdClose () {
      this.$refs.form.resetFields()
      this.$emit('closeDlg')
    },
    cmdSubmit: _.debounce(function () {
      this.$refs.form.validate((valid) => {
        if (!valid) return false
        let msg = '是否确认提交!'
        this.confirm(msg, () => {
          let params = {
            projectType: this.formData.type
          }
          modifyProjectType(params).then(res => {
            if (res.code == 200) {
              this.alertok('提交成功', () => {
                this.cmdClose()
              })
            } else {
              swal('错误', res.message || '网络连接错误', 'warning')
            }
          }).catch((err) => {
            swal('错误', '网络连接错误', 'error')
          })
        })
      })
    }, 800),
    cancel () {
      this.cmdClose()
    }
  }
}
</script>